let path=require('path')
let {createGoods,updateGoods,removeGoods,restoreGoods,find}=require('../server/goods.js')
function upload(ctx){
    try{
        let {file}=ctx.request.files
        let types=['image/jpeg','image/png']
        if(file){
            if(types.includes(file.type)){
                ctx.body={
                    code:200,
                    message:"上传成功",
                    result:{
                        path:path.basename(file.path)   //文件的相对路径
                    }
                }
            }else{
                ctx.app.emit('error',{
                    code:40000,
                    message:"图片格式不正确",
                    result:""
                },ctx)
                return
            }
        }else{
             ctx.app.emit('error',{
                 code:40000,
                 message:"上传失败",
                 result:""
             },ctx)
             return
        }
    }catch(err){
        ctx.body={
            code:50000,
            message:err,
            result:''
        }
    }
    

   
}

async function createGood(ctx){
   

   try{
    let {name,price,num,img}=ctx.request.body
     let res=await createGoods({name,price,num,img})
     if(res){
        ctx.body={
            code:200,
            message:"发布成功",
            result:{
                goods:res
            }
        }
     }else{
        ctx.app.emit('error',{
            code:50000,
            message:"商品发布失败",
            result:""
        },ctx)
     }
   }catch(error){
       console.log(error)
       ctx.app.emit('error',{
           code:50000,
           message:"商品发布失败"+error,
           result:""
       },ctx)
   }
}

async function updateGood(ctx){
   try{
    console.log(ctx.params.id,ctx.request.body,'chh')
     let res=await updateGoods(ctx.params.id,ctx.request.body)
     if(res){
         ctx.body={
             code:200,
             message:'商品跟新成功',
             result:{
                 res
             }
         }
     }else{
        ctx.app.emit('error',{
            code:50000,
            message:"商品更新失败"+error,
            result:''
        },ctx)
     }
   }catch(error){
       console.log(error)
       ctx.app.emit('error',{
           code:50000,
           message:"商品更新失败"+error,
           result:''
       },ctx)
   }
}
async function remove(ctx){
   try{
     let res=await removeGoods(ctx.params.id)
     if(res){
         ctx.body={
             code:200,
             message:"商品下架成功",
             result:""
         }
     }else{
        ctx.app.emit('error',{
            code:50000,
            message:"商品下架失败",
            result:''
        },ctx)
     }
   }catch(error){
       console.log(error+'商品下架失败')
       ctx.app.emit('error',{
           code:50000,
           message:"商品下架失败"+error,
           result:''
       },ctx)
   }
}
async function restore(ctx){
    try{
        let res=await restoreGoods(ctx.params.id)
        if(res){
           ctx.body={
               code:200,
               message:"商品上架成功",
               result:""
           }
        }else{
            ctx.app.emit('error',{
                code:50000,
                message:"商品上架失败",
                result:''
            },ctx)
        }
    }catch(error){
        ctx.app.emit('error',{
            code:50000,
            message:"商品上架失败"+error,
            result:''
        },ctx)
    }
}
async function findAll(ctx){
    try{
       let {pageSize:size,pageNum:num}=ctx.request.body
       let pageSize=size||10,
           pageNum=num||1
       let res=await find(pageSize,pageNum)
       ctx.body={
           code:200,
           message:"查询成功",
           result:{
               goods:res
           }
       }
    }catch(error){
        console.log(error)
        ctx.app.emit('error',{
            code:50000,
            message:"查询失败",
            result:''
        },ctx)
    }
}
module.exports={
    upload,
    createGood,
    updateGood,
    remove,
    restore,
    findAll
}